﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Common
{



    public class Cls_GeneralChartBackImage:BaseNode
    {
        public Cls_GeneralChartBackImage(XmlNode Node)
            : base(Node)
        {
            if (Node == null)
            {
                throw new XmlException("Node is equal null");
            }
            _XmlNode = Node;
        }

        public string ImagePath
        {
            get
            {
                XmlNode x = _XmlNode["ImagePath"];
                if (null != x)
                {
                    return _XmlNode["ImagePath"].InnerText;
                }
                else
                {
                    return null;
                }
              
            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["ImagePath"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("ImagePath"));
                _XmlNode["ImagePath"].InnerText = value.ToString();
            }
        }

        private List<Cls_PicPoint> _list = null;

        public List<Cls_PicPoint> PicPoints
        {
            get
            {
               // List<BaseNode> lst = new List<BaseNode>();
                Cls_PicPoint c = null;

                if (null != _list)
                    _list.Clear();
                else
                    _list = new List<Cls_PicPoint>();

                foreach (XmlNode x in _XmlNode.SelectNodes("PicPoint"))
                {
                    c = new Cls_PicPoint(x);
                  //  BaseNode b = c as BaseNode;
                    _list.Add(c);
                }
                //lst.Sort(delegate(BaseNode x, BaseNode y) { return x.id.CompareTo(y.id); });
                //foreach (BaseNode b in lst)
                //{
                //    _list.Add(b as Cls_Channel);
                //}
                _list.Sort();
                return _list;
            }
        }

        public Cls_PicPoint PicPoint(int id)
        {
            XmlNode ch;
            XmlDocument doc = this._XmlNode.OwnerDocument;

            //add new task
            ch = _XmlNode.SelectSingleNode("PicPoint[@id=" + id.ToString() + "]");
            if (ch == null) return null;

            return new Cls_PicPoint(ch);
        }

        public void Sort()
        {
 
        }
        //Add channel
        public Cls_PicPoint AddPicPoint(int id)
        {
            //find 
            XmlDocument doc = _XmlNode.OwnerDocument;
            XmlNode ch = _XmlNode.AppendChild(doc.CreateElement("PicPoint"));

            //find a new id
            int Id = 1;
            do
            {
                if (PicPoint(id) == null) break;
                Id++;
            } while (true);

            //add new task

            ch.Attributes.Append(doc.CreateAttribute("id")).Value = id.ToString();

            //set task property
            ch.AppendChild(doc.CreateElement("Name")).InnerText = "Mark "+id;

            ch.AppendChild(doc.CreateElement("ChannelName")).InnerText = "False";
            ch.AppendChild(doc.CreateElement("ChannelID")).InnerText = "False";
            ch.AppendChild(doc.CreateElement("X")).InnerText = "";
            ch.AppendChild(doc.CreateElement("Y")).InnerText = "";
       

            return new Cls_PicPoint(ch);
        }

        public void DeletePicPoint(int id)
        {
                          //find point
            Cls_PicPoint pt = PicPoint(id);
                if (pt == null) return;

                //delete
                pt.Node.ParentNode.RemoveChild(pt.Node);

                //re alloc id

                int i = 0;
                foreach (Cls_PicPoint p in PicPoints)
                {
                    p.id = i;
                    i++;
                }
        }
    }


    public class Cls_PicPoint : BaseNode
    {
        public Cls_PicPoint(XmlNode Node)
            : base(Node)
        {
            if (Node == null)
            {
                throw new XmlException("Node is equal null");
            }
            _XmlNode = Node;
        }

        public string ChannelName
        {
            get
            {
                return _XmlNode["ChannelName"].InnerText;
            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["ChannelName"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("ChannelName"));
                _XmlNode["ChannelName"].InnerText = value.ToString();
            }
        }

        public int ChannelID
        {
            get
            {
                if (_XmlNode == null)
                {
                    return 0;
                }
                return int.Parse(_XmlNode["ChannelID"].InnerText);
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("ChannelID");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("ChannelID"));
                Node.InnerText = value.ToString();
            }
        }

        public int CardID
        {
            get
            {
                if (_XmlNode == null)
                {
                    return 0;
                }
                if (_XmlNode["CardID"] == null)
                {
                    return 0;
                }
                return int.Parse(_XmlNode["CardID"].InnerText);
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("CardID");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("CardID"));
                Node.InnerText = value.ToString();
            }
        }



        public int X
        {
            get
            {
                if (_XmlNode == null)
                {
                    return 0;
                }
                return int.Parse(_XmlNode["X"].InnerText);
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("X");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("X"));
                Node.InnerText = value.ToString();
            }
        }

        public int Y
        {
            get
            {
                if (_XmlNode == null)
                {
                    return 0;
                }
                return int.Parse(_XmlNode["Y"].InnerText);
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("Y");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("Y"));
                Node.InnerText = value.ToString();
            }
        }

        public int Height
        {
            get
            {
                if (_XmlNode == null)
                {
                    return 0;
                }
                return int.Parse(_XmlNode["Height"].InnerText);
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("Height");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("Height"));
                Node.InnerText = value.ToString();
            }
        }

        public int Width
        {
            get
            {
                if (_XmlNode == null)
                {
                    return 0;
                }
                return int.Parse(_XmlNode["Width"].InnerText);
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("Width");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("Width"));
                Node.InnerText = value.ToString();
            }
        }

        public string FunctionName
        {
            get
            {
               XmlNode x = _XmlNode["FunctionName"];
               if (x != null)
               {
                   return x.InnerText;
               }
               else
               {
                   return "";
               }
         
            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["FunctionName"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("FunctionName"));
                _XmlNode["FunctionName"].InnerText = value.ToString();
            }
        }

        ///功能类型
        public string FunctionType
        {
            get
            {
                XmlNode x = _XmlNode["FunctionType"];
                if (x != null)
                {
                    return x.InnerText;
                }
                else
                {
                    return "";
                }

            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["FunctionType"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("FunctionType"));
                _XmlNode["FunctionType"].InnerText = value.ToString();
            }
        }
      


        //慢信号
        public string SlowSignal
        {
            get
            {
                return _XmlNode["SlowSignal"].InnerText;
            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["SlowSignal"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("SlowSignal"));
                _XmlNode["SlowSignal"].InnerText = value.ToString();
            }
        }



    }
}
